package com.cdut.cn.boke.controller;

import com.cdut.cn.boke.dao.BlogDao;
import com.cdut.cn.boke.pojo.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/")
public class BlogController {

    @Autowired
    BlogDao blogService;
/**
 *  获取博客列表
* */
    @GetMapping
//    Model用于向视图传递数据
    public String getBlogs(Model model) {
        List blogList=blogService.selectBlog();
        model.addAttribute("blogList",blogList);//将数据传递给页面，通过键 blogList
        return "index";//跳转到index的模板也就是index.html
    }

    /**
     *通过博客id获取数据
     */
    @GetMapping("/blog")
//    @RequestParam获取get请求中参数叫id的值
    public String getBlog(Model model, @RequestParam(value = "id")  String blog_id) {
        Blog blog = blogService.selectBlogByID(Integer.parseInt(blog_id));
        model.addAttribute("blog",blog);
        return "blog_detail";//跳转到blog_detail.html
    }

    /**
     *关于页面
     */
    @GetMapping("/about")
//    @RequestParam获取get请求中参数叫id的值
    public String about(Model model) {

        model.addAttribute("about","老铁666");
        return "about";//跳转到blog_detail.html
    }
}
